﻿angular
	.module('src.folders.expandableFolderController', [])
	.controller('ExpandableFolderController', ['$scope', function ($scope) {
		$scope.expanded = false;

		$scope.emailsToRead = function () {
			return $scope.folder.NumberOfUnreadedEmails + numberOfUnreadedEmailsInChildFolders($scope.folder.ChildFolders);
		};

		$scope.toggleExpansion = function () {
			$scope.expanded = !$scope.expanded;
		};

		function numberOfUnreadedEmailsInChildFolders(folders) {
			var result = 0;
			var length = folders.length;

			for (var i = 0; i < length; i++) {
				result += folders[i].NumberOfUnreadedEmails + numberOfUnreadedEmailsInChildFolders(folders[i].ChildFolders);
			}

			return result;
		}
	}]);